﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Transactions;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;

using Demo.Data.Abstract;
using Demo.Model.EntityModels;

namespace Demo.Data.Concrete {
	public abstract class BasePersonData : Database<Person>, IBaseData<Person> {
		
		/// <summary>
        /// Create Person from IDataReader
        /// </summary>
        /// <param name="reader">IDataReader</param>
        /// <returns>Person</returns>
        public virtual Person CreateObjectFromReader(IDataReader reader)
        {
            Person item = new Person();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id"))) item.id = Convert.ToInt32(reader["id"]);
                if (!reader.IsDBNull(reader.GetOrdinal("name"))) item.name = Convert.ToString(reader["name"]);
            }
            catch (Exception ex){
				throw ex;
            }
            return item;
        }
		
		/// <summary>
        /// Insert new Person
        /// </summary>
        /// <param name="t">Person</param>
       	/// <returns>ID of inserted Person</returns>
        public virtual int Insert(Person t)
        {
			int id = 0;
            Dictionary<string, object> outputValues = new Dictionary<string, object> { { "@id", null } };
            try{
                ExecuteNoneQuery("[dbo].spPerson_Insert"
                    , CreateObjectFromReader
                    , outputValues
					, t.id
					, t.name
                 );
               	id = Convert.ToInt32(outputValues["@id"]);
            }
            catch(Exception ex){
                throw ex;
            }
			return id;
        }
		
		/// <summary>
        /// Update Person
		/// </summary>
        /// <param name="t">Person</param>
        /// <returns>Number of items updated</returns>
 		public virtual int Update(Person t)
        {
		 	int effectedRecords = 0;
            try{
                effectedRecords = effectedRecords = ExecuteNoneQuery("[dbo].spPerson_Update"
                    , CreateObjectFromReader
                    , null
					, t.id
					, t.name
                );
            }
            catch(Exception ex){
                throw ex;
            }
            return effectedRecords;
        }

		/// <summary>
        /// Delete Person by primary keys
        /// </summary>
        /// <param name="useTransaction">This action  will use transaction?</param>
        /// <param name="keys">Array of primary keys</param>
        /// <returns>Number of items deleted</returns>
        public virtual int Delete(params object[] keys)
        {
			int effectedRecords = 0;
            try{
                effectedRecords = ExecuteNoneQuery("[dbo].spPerson_Delete"
                    , CreateObjectFromReader
                    , null
                    , keys
                );
            }
            catch(Exception ex){
                throw ex;
            }
            return effectedRecords;
        }
		
		/// <summary>
        /// Get list of Person
        /// </summary>
        /// <param name="orderBy">Order by string, ex: "PRODUCT_ID ASC"</param>
        /// <param name="page">Index of current page</param>
        /// <param name="pageSize">Number of items  one page</param>
        /// <param name="totalRecords">Total number of items all pages</param>
        /// <returns>List of Person</returns>
	    public virtual List<Person> GetList(string orderBy, int page, int pageSize, out int totalRecords)
        {
			 try{
                Dictionary<string, object> outputValues = new Dictionary<string, object> { { "@TotalRecords", null } };
                List<Person> lst = GetList("[dbo].spPerson_GetList"
                    , CreateObjectFromReader
                    , outputValues
                    , orderBy, page, pageSize, null);
                totalRecords = Convert.ToInt32(outputValues["@TotalRecords"]);
                return lst;
            }
            catch (Exception ex){
                throw ex;
            }
        }

		/// <summary>
        /// Get Person by primary keys
        /// </summary>
        /// <param name="keys">Array of primary keys</param>
        /// <returns>Person </returns>
       	public virtual Person Get(params object[] keys)
        {
			try{
                Person t = Get("[dbo].spPerson_Get"
                    , CreateObjectFromReader
                    , null
                    , keys);
                return t;
            }
            catch (Exception ex){
                throw ex;
            }
        }

	}
}